package struts2.sample07.action;

import org.apache.struts2.config.Result;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import struts2.sample07.mapping.User;

/**
 * 利用者マスタ新規登録アクション
 */
@Result("user_master_create.jsp")
public class UserMasterCreateAction {

	private User user;
	
	/** hibernateのセッションファクトリー */
	private SessionFactory sessionFactory;


	/**
	 * 初期表示メソッド
	 */
	public String init(){
		// 利用者プロパティを初期化
		user = new User();

		return "success";
	}

	
	
	/**
	 * 利用者を追加するメソッド
	 */
	public String create(){
		Session dbSession = sessionFactory.openSession();
		
		System.out.println(user.getId());
		System.out.println(user.getPassword());
		
		try{
			Transaction transaction = dbSession.beginTransaction();
			dbSession.save(user);
			transaction.commit();
		}finally{
			dbSession.close();
		}
		
		return "success";
	}
	

	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
}
